package com.ysyx.ysyx_nowtrack.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.ysyx.ysyx_nowtrack.dto.BaseDto;
import com.ysyx.ysyx_nowtrack.qo.OrderinfQo;
import com.ysyx.ysyx_nowtrack.service.TrackPojoService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 *
 * @description: xxxx接口/类
 * @author:薛宇晖
 * @create:2023/4/12 14:37
 */
@RestController
public class WriteTrackController {

    @Autowired
    private RedissonClient redissonClient;


    @Autowired
    private TrackPojoService trackPojoService;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /*
     * @Description: (作用)记录和写入数据库
     * @params: qo 经度 纬度 司机id
     * @return:
     * @Author: 薛宇晖
     * @create:2023/4/12
     */

    @RequestMapping("/track/redistrack/toRedis")
    private BaseDto writeOrderTrackyoRedis(@RequestBody OrderinfQo qo){
        System.out.println(qo);
        BaseDto baseDto = trackPojoService.writeTrack(qo);
        return baseDto;
    }



    @RequestMapping(value = "/track/writeToMysql/{orderId}",method = RequestMethod.GET)
    public void  writeTrackToMysql(@PathVariable String orderId){
        //去redis中获取redis的所有轨迹
//        trackPojoService.mqWriteTrack(orderId);
        System.err.println("进来了");
        System.out.println(orderId);
            rocketMQTemplate.convertAndSend("track:write",orderId);
    }

    /*
     * @Description: (作用)
     * @params: qo    driverId   经纬度
     * @return:  void
     * @Author: 薛宇晖
     * @create:2023/4/13
     */
    @RequestMapping("/track/driver/sendtrack")
    public void pushDriverPoint(@RequestBody OrderinfQo qo){
        //我再redis的区块桶中判断位置
        trackPojoService.pushDriverPoint(qo);
//        String areaName = "";
//
//        List<RMap<Object,Object>> maps = new ArrayList<>();
//
//
//        RMap<Object, Object> mapA1 = redissonClient.getMap("area:A1");
//        RMap<Object, Object> mapA2 = redissonClient.getMap("area:A2");
//        RMap<Object, Object> mapA3 = redissonClient.getMap("area:A3");
//        RMap<Object, Object> mapB1 = redissonClient.getMap("area:B1");
//        RMap<Object, Object> mapB2 = redissonClient.getMap("area:B2");
//        RMap<Object, Object> mapB3 = redissonClient.getMap("area:B3");
//        maps.add(mapA1);
//        maps.add(mapA2);
//        maps.add(mapA3);
//        maps.add(mapB1);
//        maps.add(mapB2);
//        maps.add(mapB3);
//
//        //判断司机的位置
//        for (RMap<Object, Object> map : maps) {
//
//            String  theArea = map.getName();
//            if (redissonClient.getGeo(theArea+"drivers").pos(qo.getDriverId()).get(qo.getDriverId())!=null){
//                //每个地区池子去判断之前的地理信息
//                boolean remove = redissonClient.getGeo(theArea + "drivers").remove(qo.getDriverId());
//                break;
//            }
//        }
//
//
//        //判断司机的位置
//        for (RMap<Object, Object> map : maps) {
//
//            Double leftupLongitude = Double.parseDouble((String) map.get("leftupLongitude")) ;
//            Double leftupLatitude = Double.parseDouble((String) map.get("leftupLatitude")) ;
//            Double rightdownLongitude = Double.parseDouble((String) map.get("rightdownLongitude")) ;
//            Double rightdownLatitude = Double.parseDouble((String) map.get("rightdownLatitude")) ;
//            if (qo.getLongitude()>=leftupLongitude &&   //司机位置经度 >= 区域左上经度
//                    qo.getLongitude()<=rightdownLongitude &&  //司机位置经度 <= 区域右下经度
//                    qo.getLatitude()>=rightdownLatitude &&// 司机位置纬度 >= 区域右下纬度
//                    qo.getLatitude()<=leftupLatitude// 司机位置纬度 >= 区域左上纬度
//            )
//            {
//                areaName =  map.getName();
//                break;
//            }else {
//                continue;
//            }
//        }
//        //得删除之前的定位
//
//
//
//
//
//        //获取到司机所在地区的名称 areaname
//        System.out.println(areaName);
//
//        RGeo<String> geo = redissonClient.getGeo(areaName+":drivers");
//        geo.add(new GeoEntry(qo.getLongitude(),qo.getLatitude(),qo.getDriverId()));
    }

}
